/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	createXULElement: function(tagName) {
		return (document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",tagName));
	},
	
	//compare to accesxt_generateTreeboxResults
	fillTreeboxWithData: function(obj, columnNames) {
		try {
			var item, child;
			var row = createXULElement("treerow");
		
			for (var col in columnNames) {
				if (typeof(columnNames[col])=='string') {			
					var colName = columnNames[col];
					child = createXULElement("treecell");
					var value = (obj[colName] == null) ? "" : obj[colName].toString();
					child.setAttribute("label",value);
					
					row.appendChild(child);
				}
			}
			item = createXULElement("treeitem");
			item.appendChild(row);
			setRowColor(obj, item);
		} catch (ex) {
			accessext_consoleDump('fillTreeboxWithData exception: ', ex);
		}
		return item;
	},
	
	createTree: function(label, obj, onselecthandler, attachTo) {
		var child;
		var data;
		var columnData = obj[0].displayDetails.dataElementNames;
		var columnNames = obj[0].displayDetails.columnHeaders;
		var labelNoSp = label.replace(/\s+/g, '-');
	
		try {
			var row = createXULElement("treecols");
			row.setAttribute("id", labelNoSp + "-treecols" );
	
			for (var col in columnNames) {
				if (typeof(columnNames[col])=='string') {			
					child = createXULElement("treecol");
					child.setAttribute("label",accessext_getBundleSingleString(columnNames[col]));
					//child.setAttribute("label",columnNames[col]);
					child.setAttribute("id",labelNoSp + "-list-header-" + columnData[col]);
					child.setAttribute("flex","1");
					child.setAttribute("sortDirection","");
					child.setAttribute("hidden","true");
					row.appendChild(child);
					var splitter = createXULElement("splitter");
					splitter.setAttribute("class","tree-splitter");
					row.appendChild(splitter);
				}
			}
			var tree = createXULElement("tree");
			if (onselecthandler != '') {
				tree.setAttribute("id","treebox");
				tree.setAttribute("onselect",onselecthandler);
			}
			tree.setAttribute("flex","5");
			tree.setAttribute("enableColumnDrag","true");
			tree.setAttribute("seltype","single");
			tree.appendChild(row);
			tree.setAttribute("onblur", "AccessibilityExtension.LIB.Inspector_saveDialogPrefs();" ); 
			
			var treeChildren = createXULElement("treechildren");
			
			for (var i = 0; i < obj.length; i++) {
				data = fillTreeboxWithData(obj[i], columnData);
				treeChildren.appendChild(data);
			}
			tree.appendChild(treeChildren);
			
			var groupbox = createXULElement("groupbox");
			groupbox.setAttribute("flex","2");
			var caption = createXULElement("caption");
			caption.setAttribute("label",label);
			groupbox.appendChild(caption);
			groupbox.appendChild(tree);
			attachTo.appendChild(groupbox);
			return groupbox;
			
		} catch (ex) {
			accessext_consoleDump('createTree exception: ', ex);
		}
	},
	
	createTreeitem: function() {
		var treeitem = createXULElement("treeitem");
		treeitem.setAttribute("container","true");
		treeitem.setAttribute("open","true");				
		return treeitem;
	},
	
	createTreecell: function(text) {
		var treecell = createXULElement("treecell");
		treecell.setAttribute("label",text);
		return treecell;
	},
	
	createTextboxWithLabel: function(labeltext, textboxvalue, multilinevalue, attachTo) {
	    //   <xul:label control="widget-description" id="widget-description-label" value="&focusinspector.description.hbox.label;" style="width: 3em;"/>
	    //   <xul:textbox id="widget-description-textbox" flex="5" readonly="true" minwidth="200" multiline="false" />
		var hbox = createXULElement("hbox");
		var label = createXULElement("label");
		var textbox = createXULElement("textbox");
		
		label.setAttribute("value",labeltext);
		label.setAttribute("minwidth","80");	
		textbox.setAttribute("value",textboxvalue);	
		textbox.setAttribute("readonly","true");	
		textbox.setAttribute("flex","5");
		textbox.setAttribute("multiline",multilinevalue);	
		textbox.setAttribute("minwidth","200");	
					
		hbox.appendChild(label);
		hbox.appendChild(textbox);
		attachTo.appendChild(hbox);
	},
	
	createAccessibilityInformationBox: function(textboxvalue) {
		try {
			var groupbox = createXULElement("groupbox");
			groupbox.setAttribute("flex","1");
			var caption = createXULElement("caption");
			caption.setAttribute("id","groupbox-note-caption");
			caption.setAttribute("label", getAccesessibilityInfoCaption()); //accessext_setNoteCaption();
			groupbox.appendChild(caption);
		
			var textbox = createXULElement("textbox");
			textbox.setAttribute("id","label-note-text");
			textbox.setAttribute("value",textboxvalue);	
			textbox.setAttribute("readonly","true");	
			textbox.setAttribute("multiline","true");	
			textbox.setAttribute("flex","1");
			groupbox.appendChild(textbox);
		} catch (ex) {
			accessext_consoleDump('createAccessibilityInformationBox exception: ', ex);
		}
		return groupbox;
		
	},
	createHboxButton: function(labeltext, disabled, actionfunc, accesskey, attachTo) {
		var hbox = createXULElement("hbox");
		hbox.setAttribute("align","right");
		var button = createButton(labeltext, disabled, actionfunc, accesskey);
		hbox.appendChild(button);
		attachTo.appendChild(hbox);
	},
	createButton: function(labeltext, disabled, actionfunc, accesskey) {
		var button = createXULElement("button");
		button.setAttribute("id",labeltext);
		button.setAttribute("label",labeltext);
		button.setAttribute("oncommand",actionfunc);
		if (accesskey != '') button.setAttribute("accesskey",accesskey);
		button.setAttribute("disabled",disabled);
		return button
	}
});
}});